package com.gaofei.miaosha.controller;


import com.gaofei.miaosha.domain.Storage;
import com.gaofei.miaosha.service.IStorageService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaofei
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    IStorageService storageService;

    @RequestMapping("placeAnOrder")
    public Object placeAnOrder(Integer goodsId)  {
        //先查询原有库存
        Storage s = storageService.getById(goodsId);

        //乐观锁使用注意事项
        //支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
        //整数类型下 newVersion = oldVersion + 1
        //newVersion 会回写到 entity 中
        //仅支持 updateById(id) 与 update(entity, wrapper) 方法
        //在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
        //如果库存>=1 才能下单
        if(s.getCount()>=1){
            s.setCount(s.getCount() - 1);
            //这个方法乐观锁就生效了
            storageService.updateById(s);
        }

        return "下单success";
    }
}

